package com.systop.party05.activity;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.systop.party05.R;
import com.systop.party05.base.BaseActivity;
import com.systop.party05.fragment.ActivityFragment;
import com.systop.party05.fragment.FeeFragment;
import com.systop.party05.fragment.HomeFragment;
import com.systop.party05.fragment.PersonFragment;
import com.systop.party05.fragment.SchoolFragment;
import com.systop.party05.utils.ScreenManagerUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends BaseActivity {

    private long exitTime=0;//记录上次单击返回按钮的时间
   private  int index=0;//单击页卡索引
    private int currentTabIndex=-1;//当前页卡索引
    private Fragment[] fragments;

    @BindView(R.id.main_fl_content)
    FrameLayout mainFlContent;
    @BindView(R.id.main_iv_1)
    ImageView mainIv1;
    @BindView(R.id.main_tv_1)
    TextView mainTv1;
    @BindView(R.id.main_ll_1)
    LinearLayout mainLl1;
    @BindView(R.id.main_iv_2)
    ImageView mainIv2;
    @BindView(R.id.main_tv_2)
    TextView mainTv2;
    @BindView(R.id.main_ll_2)
    LinearLayout mainLl2;
    @BindView(R.id.main_iv_3)
    ImageView mainIv3;
    @BindView(R.id.main_tv_3)
    TextView mainTv3;
    @BindView(R.id.main_ll_3)
    LinearLayout mainLl3;
    @BindView(R.id.main_iv_4)
    ImageView mainIv4;
    @BindView(R.id.main_tv_4)
    TextView mainTv4;
    @BindView(R.id.main_ll_4)
    LinearLayout mainLl4;
    @BindView(R.id.main_iv_5)
    ImageView mainIv5;
    @BindView(R.id.main_tv_5)
    TextView mainTv5;
    @BindView(R.id.main_ll_5)
    LinearLayout mainLl5;
    @BindView(R.id.main_ll_bottom)
    LinearLayout mainLlBottom;
    @BindView(R.id.main_iv_33)
    ImageView mainIv33;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setBaseContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initData();
    }

    @OnClick({R.id.main_ll_1, R.id.main_ll_2, R.id.main_ll_3, R.id.main_ll_4, R.id.main_ll_5})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.main_ll_1:
                //首页
                index=0;
                break;
            case R.id.main_ll_2:
                //党建活动
                index=1;
                break;
            case R.id.main_ll_3:
                //网上党校
                index=2;
                break;
            case R.id.main_ll_4:
                //我的党费
                index=3;
                break;
            case R.id.main_ll_5:
                //个人中心
                index=4;
                break;
        }
         fragmentControl();
    }


    public void initData() {
        HomeFragment homeFragment = new HomeFragment();
        ActivityFragment activityFragment = new ActivityFragment();
        SchoolFragment schoolFragment = new SchoolFragment();
        FeeFragment feeFragment = new FeeFragment();
        PersonFragment myFragment = new PersonFragment();

        fragments = new Fragment[]{homeFragment, activityFragment, schoolFragment, feeFragment, myFragment};
        fragmentControl();
    }

    /**
     * 控制fragment的变化
     */
    public void fragmentControl() {
        if (currentTabIndex != index) {
            setBottomColor();
            FragmentTransaction trx = getSupportFragmentManager().beginTransaction();
            if (currentTabIndex != -1) {
                trx.hide(fragments[currentTabIndex]);
            }
            if (!fragments[index].isAdded()) {
                trx.add(R.id.main_fl_content, fragments[index]);
            }
            trx.show(fragments[index]).commit();
            currentTabIndex = index;
        }
    }



    /**
     * 点击底部栏按钮变为选中状态
     */
    private void setBottomColor() {
        removeBottomColor();
        switch (index) {
            case 0:
                mainIv1.setImageResource(R.mipmap.tab_home_selected);
                mainTv1.setTextColor(getResources().getColor(R.color.colorPrimary));
                break;
            case 1:
                mainIv2.setImageResource(R.mipmap.tab_activity_selected);
                mainTv2.setTextColor(getResources().getColor(R.color.colorPrimary));
                break;
            case 2:
                mainTv3.setTextColor(getResources().getColor(R.color.colorPrimary));
                break;
            case 3:
                mainIv4.setImageResource(R.mipmap.tab_dues_selected);
                mainTv4.setTextColor(getResources().getColor(R.color.colorPrimary));
                break;
            case 4:
                mainIv5.setImageResource(R.mipmap.tab_person_selected);
                mainTv5.setTextColor(getResources().getColor(R.color.colorPrimary));
                break;
        }
    }

    /**
     * 清除底部栏颜色
     */
    private void removeBottomColor() {
        switch (currentTabIndex) {
            case 0:
                mainIv1.setImageResource(R.mipmap.tab_home);
                mainTv1.setTextColor(getResources().getColor(R.color.c_333333));
                break;
            case 1:
                mainIv2.setImageResource(R.mipmap.tab_activity);
                mainTv2.setTextColor(getResources().getColor(R.color.c_333333));
                break;
            case 2:
                mainTv3.setTextColor(getResources().getColor(R.color.c_333333));
                break;
            case 3:
                mainIv4.setImageResource(R.mipmap.tab_dues);
                mainTv4.setTextColor(getResources().getColor(R.color.c_333333));
                break;
            case 4:
                mainIv5.setImageResource(R.mipmap.tab_person);
                mainTv5.setTextColor(getResources().getColor(R.color.c_333333));
                break;
        }
    }


    /**
     * 重写物理返回按键点击事件，连续点击两次间隔不超过2s,则退出应用
     * @param keyCode
     * @param event
     * @return
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event){
        if(keyCode==KeyEvent.KEYCODE_BACK&&event.getAction()==KeyEvent.ACTION_DOWN){
              if(System.currentTimeMillis()-exitTime>2000){
                  showMessage("再按一次退出程序");
                  exitTime=System.currentTimeMillis();
              }else{
                  ScreenManagerUtils.getInstance().finishAllActivityAndClose();
              }
              return true;
        }
        return super.onKeyDown(keyCode,event);
    }
}